﻿namespace LS.Nc6.Web.Entry.Areas.Cms.Controllers
{
    /// <summary>
    /// 广告管理
    /// </summary>
    [Route("[area]/[controller]/[action]")]
    [Area("Cms")]
    [ApiDescriptionSettings("Default")]
    public class BannerController   : BaseAdminController
    {
        #region BannerController

        private readonly ICmsBannerService _bannerService;
        private readonly ISysOperateLogService _operatelogService;
        private readonly ISysFileService _fileService;
        private readonly ISysDictionaryService _dictionaryService;
        private readonly ICache _cache;
        public BannerController(ICmsBannerService bannerService, ISysFileService fileService, ISysDictionaryService dictionaryService, ISysOperateLogService operatelogService,  IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed)
              : base(cacheOptions, resolveNamed)
        {
            _bannerService = bannerService;
            _operatelogService = operatelogService;
            _fileService = fileService;
            _dictionaryService = dictionaryService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
        }
        #endregion

        #region 页面
        public ActionResult Index()
        {
            SysPower(SysMenuConstant.MENU_CMS_BANNER_GL, SysMenuConstant.FUN_SELECT, UserInfo.ID.ToString());
            return View();
        }
        public async  Task<ActionResult> Create()
        {
            SysPower(SysMenuConstant.MENU_CMS_BANNER_GL, SysMenuConstant.FUN_SELECT, UserInfo.ID.ToString());
            var model = new CmsBannerEntity();
            ViewBag.BannerTypeList = await _dictionaryService.DictionaryTagByDicType(SysDictionaryType.ENUM_BANNERTYPE);
            return View("Edit", model);
        }
        public async Task<ActionResult> Edit(int id)
        {
            SysPower(SysMenuConstant.MENU_CMS_BANNER_GL, SysMenuConstant.FUN_UPDATE, UserInfo.ID.ToString());
            var model = await _bannerService.GetByIdAsync(id);
            if (model == null)
            {
                return ErrorCustomMsg(ResponseHelper.NONEXISTENT);
            }
            ViewBag.BannerTypeList = await _dictionaryService.DictionaryTagByDicType(SysDictionaryType.ENUM_BANNERTYPE);
            return View(model);
        }
        #endregion

        #region  公共方法
        [HttpGet]
        public async Task<ResponseResultDataList> GetList()
        {
            string keyword = RequestHelper.FilterParam(Request.Query["title"]);
            var page = Request.Query["page"].ToString().GetQueryValueOrNull<int>(1);
            var limit = Request.Query["limit"].ToString().GetQueryValueOrNull<int>(20);
            string field = Request.Query["field"];
            string order = Request.Query["order"];

            var expable = Expressionable.Create<CmsBannerEntity, SysDictionaryEntity>();
            expable.AndIF(!string.IsNullOrEmpty(keyword), (a,b) => a.Title.Contains(keyword));
            var exp = expable.ToExpression();
            var strField = RequestHelper.FilterParam(field);
            var strOrder = RequestHelper.FilterParam(order);

            Expression<Func<CmsBannerEntity, SysDictionaryEntity, object>> expressionOrder;
            if (string.IsNullOrEmpty(strField) || string.IsNullOrEmpty(strOrder))
            {
                expressionOrder = (a,b) => new { a.Taxis,a.Id };
            }
            else
            {
                //实现字段排序
                expressionOrder = (a,b) => new { a.CreateDate };
            }
            var result = await _bannerService.PageDynamicLeftJoinList(exp, page, limit, expressionOrder, (a,b) => new {
                id = a.Id,
                title = a.Title,
                bTypeName = b.Dname,
                bType = a.BType,
                urlPath = a.UrlPath,
                bhref = a.Bhref,
                taxis =a.Taxis,
                createDate = a.CreateDate
            });
            return new ResponseResultDataList { code = ResponseHelper.SEUCCESSCODE, message = "请求成功", data = result.Item1, count = result.Item2 };
        }



        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> SaveData([FromBody] CmsBannerEntity model)
        {
            bool power = true;
            long result = 0;
            if (model.Id > 0)
            {
                power = SysPower(SysMenuConstant.MENU_CMS_BANNER_GL, SysMenuConstant.FUN_UPDATE, UserInfo.ID.ToString(), false);
                if (!power)
                {
                    return ResponseHelper.MessageResult(ResponseHelper.PERMISSION_QXZU);
                }
                var editmodel = await _bannerService.GetByIdAsync(model.Id);
                CommonMethodHelper.MapperToModel(editmodel, model, null);
                result = await _bannerService.UpdateAsync(editmodel);
            }
            else
            {
                power = SysPower(SysMenuConstant.MENU_CMS_BANNER_GL, SysMenuConstant.FUN_ADD, UserInfo.ID.ToString(), false);
                if (!power)
                {
                    return ResponseHelper.MessageResult(ResponseHelper.PERMISSION_QXZU);
                }

                result = await _bannerService.InsertAsync(model);
            }
            return ResponseHelper.SaveResult(result);
        }


        [HttpPost]
        public async Task<ResponseResult> Delete([FromBody] DeletDataInput input)
        {
            var r = SysPower(SysMenuConstant.MENU_CMS_BANNER_GL, SysMenuConstant.FUN_DELETE, UserInfo.ID.ToString(), false);
            if (!r)
            {
                return ResponseHelper.MessageResult(ResponseHelper.PERMISSION_WSCQX);
            }
            if (string.IsNullOrEmpty(input.Ids))
            {
                return ResponseHelper.MessageResult(ResponseHelper.PARAMETER_CSCW);
            }
            int result = await _bannerService.DeleteAsync(input.Ids.ToString());
            return ResponseHelper.DeleteResult(result);
        }

        #endregion

        #region 私有方法

        #endregion

    }
}